package com.sluggard.common.model.res;

import cn.hutool.http.HttpStatus;
import com.sluggard.common.consts.ResultConstant;
import io.swagger.v3.oas.annotations.media.Schema;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lizheng
 * @since 2024.4.17
 */

//@Schema(name = "请求响应", description = "请求响应对象")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Schema(description = "状态码")
    private Integer code;
    @Schema(description = "响应信息")
    private String message;
    @Schema(description = "响应数据")
    private T data;
    @Schema(description = "时间戳")
    private long timestamp = System.currentTimeMillis();

    public Boolean success(){
        return this.code == 200;
    }

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public static Result<Map<Object, Object>> okMap(Object key, Object value) {
        Map<Object, Object> result = new HashMap<>();
        result.put(key, value);
        return new Result<>(HttpStatus.HTTP_OK, ResultConstant.REQUEST_OK_DEFAULT_MSG, result);
    }

    public static <T> Result<T> ok(String msg) {
        return new Result<T>(HttpStatus.HTTP_OK, msg, null);
    }

    public static <T> Result<T> ok(T data, String msg) {
        return new Result<T>(HttpStatus.HTTP_OK, msg, null);
    }

    public static <T> Result<T> ok() {
        return new Result<T>(HttpStatus.HTTP_OK, ResultConstant.REQUEST_OK_DEFAULT_MSG, null);
    }

    public static <T> Result<T> ok(T data) {
        return new Result<T>(HttpStatus.HTTP_OK, ResultConstant.REQUEST_OK_DEFAULT_MSG, data);
    }

    public static <T> Result<T> error(Integer status, String msg) {
        return new Result<T>(status, msg, null);
    }

    public static <T> Result<T> error(String msg) {
        return new Result<T>(HttpStatus.HTTP_INTERNAL_ERROR, msg, null);
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}
